package com.itboye.ihomebank.login;

import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;

/**
 * 修改密码2（输入验证码）
 * 
 * @author 余 焱
 * 
 */
public class PasswordActivityTwo extends BaseOtherActivity implements
		OnClickListener, Observer {
	ImageView close_icon;// 返回图片
	TextView add_shap_title_tv;// 标题
	TextView pass_two_phone;// 显示手机号
	TextView pass_two_ok;// 下一步

	TextView pass_two_time;// 验证码重新发送
	EditText pass_code;// 验证码

	private TimeCount time;// 倒计时

	private Intent intent;

	private String phone;// 接上页面传来的手机号
	private String yzm;// 接上页面传来的验证码（暂时收不到信息，验证码从上个页面传过来）

	private UserPresenter userPresenter;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:// 返回
			finish();
			break;
		case R.id.pass_two_time:// 重新获取验证码
			getyzm();
			time.start();
			break;
		case R.id.pass_two_ok:// 下一步
			// VerificationYZM();
			Intent intent = new Intent(PasswordActivityTwo.this,
					PasswordActivityThree.class);
			intent.putExtra("phone", phone);
			intent.putExtra("yzm", yzm);
			startActivity(intent);
			PasswordActivityTwo.this.finish();
			break;

		default:
			break;
		}

	}

	/**
	 * 验证验证码是否正确
	 */
	private void VerificationYZM() {
		if (pass_code.getText().toString().trim() == null) {
			ByAlert.alert("请输入验证码");
			return;
		}
		userPresenter = new UserPresenter(this);
		userPresenter.verification(phone, "2", yzm);

	}

	/**
	 * 获取验证码
	 */
	private void getyzm() {
		userPresenter = new UserPresenter(this);
		userPresenter.send(phone, "2");

	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (!entity.getCode().equals(0)) {
				if (entity.getEventType() == UserPresenter.verification_success) {
					String bean = (String) entity.getData();
					if (bean != null) {
						ByAlert.alert("验证通过");
						Intent intent = new Intent(PasswordActivityTwo.this,
								RegisterActivityThree.class);
						intent.putExtra("phone", phone);
						intent.putExtra("yzm", yzm);
						startActivity(intent);
						PasswordActivityTwo.this.finish();
					} else {
						ByAlert.alert("验证失败");
					}
				}
				if (entity.getEventType() == UserPresenter.verification_fail) {
					ByAlert.alert("验证码错误");
				}
				if (entity.getEventType() == UserPresenter.send_success) {
					String bean = (String) entity.getData();
					if (bean != null) {
						ByAlert.alert("验证码发送成功");
						pass_code.setText(bean);
					} else {
						ByAlert.alert("失败");
					}
				}
				if (entity.getEventType() == UserPresenter.send_fail) {
					ByAlert.alert("验证码发送失败");
				}
			}
		}
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_password_two;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		add_shap_title_tv.setText("填写验证码");

		time = new TimeCount(60000, 1000, pass_two_time);// 构造CountDownTimer对象
		time.start();

		intent = super.getIntent();
		phone = intent.getStringExtra("phone");
		yzm = intent.getStringExtra("yzm");

		pass_two_phone.setText(phone);
		pass_code.setText(yzm);
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	/*	*//**
	 * 获取验证码
	 */
	/*
	 * private void getyzm() { MyJsonRequest<String> request = new
	 * MyJsonRequest.Builder<String>()
	 * .apiVer("100").typeKey("BY_Message_send_sms") .param("mobile", phone)
	 * .param("code_type", "2") .requestListener(new XRequestListener<String>()
	 * {
	 * 
	 * @Override public void onResponse(String response) {
	 * T.showLong(PasswordActivityTwo.this, response);
	 * 
	 * } }).errorListener(new XErrorListener() {
	 * 
	 * @Override public void onErrorResponse(Exception exception, int code,
	 * String msg) { if (exception instanceof CodeErrorException) {
	 * T.showLong(PasswordActivityTwo.this, msg); } else {
	 * T.showLong(PasswordActivityTwo.this, exception.toString()); }
	 * 
	 * } }).build(); HttpRequest.getDefaultRequestQueue().add(request); }
	 *//**
	 * 验证验证码是否正确
	 */
	/*
	 * private void VerificationYZM() { if
	 * (pass_code.getText().toString().trim() == null) {
	 * T.showLong(PasswordActivityTwo.this, "请输入验证码"); return; }
	 * MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
	 * .apiVer("100").typeKey("BY_Message_checkCode") .param("mobile", phone)
	 * .param("code", pass_code.getText().toString()) .param("code_type", "2")
	 * .requestListener(new XRequestListener<String>() {
	 * 
	 * @Override public void onResponse(String response) {
	 * T.showLong(PasswordActivityTwo.this, response); Intent intent=new
	 * Intent(PasswordActivityTwo.this, PasswordActivityThree.class);
	 * intent.putExtra("phone", phone); intent.putExtra("yzm",
	 * pass_code.getText().toString()); startActivity(intent);
	 * 
	 * } }).errorListener(new XErrorListener() {
	 * 
	 * @Override public void onErrorResponse(Exception exception, int code,
	 * String msg) { if (exception instanceof CodeErrorException) {
	 * T.showLong(PasswordActivityTwo.this, msg); } else {
	 * T.showLong(PasswordActivityTwo.this, exception.toString()); }
	 * 
	 * } }).build();
	 * 
	 * HttpRequest.getDefaultRequestQueue().add(request); }
	 */
}
